<?php
// +----------------------------------------------------------------------
// | 日志标签模型
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: TagModel.class.php 26 2011-02-15 05:48:40Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class TagModel extends Model {

	public $_validate	=	array(
		array('tag_name','require','{%tag_name_require}'),
		array('tag_url','require','{%tag_url_require}'),
		array('tag_name','','{%tag_name_unique}',0,'unique',1),
		array('tag_url','','{%tag_url_unique}',0,'unique',1),
	);

	public $_auto		=	array(
		array('tag_addtime','time',3,'function'),
		array('tag_url','gdToEncode',3,'callback'),
	);

	
	//编码
	protected function gdToEncode() {
		$str = $_POST['tag_url'];
		if (is_gb($str)) {
			return strtolower(urlencode($str));
		} else {
			return strtolower($str);
		}
	}
	
	/**
	 * 编辑日志的时候返回此日志的标签集,点击可以删除标签
	 *
	 * @access  public
	 * @return  array
	 */
	public function getTagFromEditPosts($id) {
	
		$sql = "select t.tag_name, t.tag_id ".
               " from ".C('DB_PREFIX')."tag as t, ".C('DB_PREFIX')."posts_tag as pt ".
               " where pt.tag_id = t.tag_id and pt.posts_id = $id";
		$res = $this->query($sql);	
		/*foreach($res as $key => $v) {
				$new .= $v['tag_name'].',';
		}
		$new = substr($new,0,-1);*/
		foreach($res as $key => $v) {
				$new[] = '<span><a href =__APP__/Posts/dropTagFromEditPosts/postsId/'.$id.'/tagId/'.$v['tag_id'].' title ="点击删除" onclick="return confirm(\'- 此操作将去掉此标签\r\n- 确定要执行操作?\');" >'.$v['tag_name'].'</a></span>';
		}
		return $new;
	}
	
	/**
	 * 连接标签日志表获取数据以二维数组返回
	 *
	 * @access  public
	 * @return  array
	 */
	public function getTagArray() {
	
		$sql = "select t.tag_id, t.tag_url, t.tag_name, t.tag_hit, t.tag_addtime".
               " from ".C('DB_PREFIX')."tag as t ".
               " group by t.tag_id ".
               ' order by t.tag_hit asc ';	
		$res = $this->query($sql);
		
		$sql = "select p.tag_id, count(*) as posts_num , po.posts_status" .
               " from ".C('DB_PREFIX')."posts_tag as p , ".C('DB_PREFIX')."tag as c ,".C('DB_PREFIX')."posts as po".
               " where p.tag_id = c.tag_id and po.posts_id = p.posts_id and po.posts_status <> 0" .
               " group by p.tp_id";
		$res2 = $this->query($sql);	
		
		$newres = array();
		foreach( $res as $k=>$v) {
			$newres[$v['tag_id']] = $v['posts_num'];
			foreach($res2 as $ks=>$vs) {
				if ($v['tag_id'] == $vs['tag_id']) {
					$newres[$v['tag_id']] += $vs['posts_num'];
				}
			}
		}

		foreach ($res as $k=>$v) {
			$res[$k]['posts_num'] = !empty($newres[$v['tag_id']]) ? $newres[$v['tag_id']] : 0;
		}
		
		return $res;
	}
	
	/**
	 * 获取标签名
	 *
	 * @access  public
	 * @param   boolean $re_type    返回的类型: 值为真时返回下拉列表,否则返回数组
	 * @return  array
	 */
	public function getTagName($re_type = true) {
		$tagname = $this->field('tag_id,tag_name')->order('tag_hit desc')->select();
		if ($re_type) {
			foreach ($tagname as $key => $s) {
			/*	foreach ($s as $c) {*/
					$select .= "<option value='{$s['tag_id']}' ";
					$select .= ">";
					$select .= "{$s['tag_name']}</option>";	
			/*	}*/
			}			
			return $select;
		} else {		
			return $tagname;
        }
	}
	
	/**
	 * 循环
	 *
	 * @access  public
	 * @param   array   $arr        一维标签名称数组
	 * @param   array   $posts_id   新插入的日志id
	 * @return  void
	 */
	public function addTagFromPosts($arr,$posts_id) {
		foreach($arr as $t) {
			$this->insertTagFromPosts($t,$posts_id);
		}
	}
  
	/**
	 * 插入标签
	 *
	 * @access  public
	 * @param   array   $arr        一维标签名称数组
	 * @param   array   $posts_id   新插入的日志id
	 * @return  void
	 */
	public function insertTagFromPosts($tag_name,$posts_id) {
	
		$pt = M ('PostsTag');
		
		$arr = $this->where("tag_name='".$tag_name."'")->field('tag_id')->find();	

		if(!empty($arr['tag_id'])) {
			if(!$pt->where("posts_id='".$posts_id."'AND tag_id='".$arr['tag_id']."'")->find()) {
				$listData['posts_id'] = $posts_id;
				$listData['tag_id']   = $arr['tag_id'];
				$pt->add($listData);
			}
		}else{
			$data['tag_name'] = $tag_name;
			$data['tag_url']  = urlencode(strtolower($tag_name));
			$data['tag_addtime'] = gmtime();
			$tag_id = $this->add($data);
			$listData['posts_id'] = $posts_id;
			$listData['tag_id']   = $tag_id;
			$pt->add($listData);
		}
	}
	
}

?>